package com.rom.cpptest;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.io.File;

public class MainActivity extends AppCompatActivity {

    // Used to load the 'native-lib' library on application startup.
    static {
        System.loadLibrary("native-lib");
    }
    class cinc
    {
        int add (int a ,int b)
        {
            Log.d("cinc.add" , String.valueOf(a+b));
            return  a+b;
        }
        void cinc()
        {
            int a=0;
            a=a+1243;
        }
    }
    public cinc c=new cinc();
    public  String  add_non_static_log(String log)
    {
        System.out.println(log);
        Log.d("MyReceiver add_non_static_log" , log);
        return log;
    }
    public static String  add_log(String log)
    {
        System.out.println(log);
        Log.d("MyReceiver" , log);
        return log;
    }
    TextView result_text;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        LinearLayout layout = new LinearLayout(this);
        super.setContentView(layout);
        layout.setOrientation(LinearLayout.VERTICAL);
        result_text = new TextView(this);
        result_text.setMaxHeight(1200);
        result_text.setMovementMethod(ScrollingMovementMethod.getInstance());

        Button bn_java_root_detect=new Button(this);
        bn_java_root_detect.setText("java root检测");
        bn_java_root_detect.setLayoutParams(new ViewGroup.LayoutParams(
                600,
                ViewGroup.LayoutParams.WRAP_CONTENT));

        Button bn_libc_root_detecct=new Button(this);
        bn_libc_root_detecct.setText("libc root检测");
        bn_libc_root_detecct.setLayoutParams(new ViewGroup.LayoutParams(
                600,
                ViewGroup.LayoutParams.WRAP_CONTENT));
        Button bn_svc_root_detect=new Button(this);
        bn_svc_root_detect.setText("svc root检测");
        bn_svc_root_detect.setLayoutParams(new ViewGroup.LayoutParams(
                600,
                ViewGroup.LayoutParams.WRAP_CONTENT));

        Button bn_svc_cpu_detect=new Button(this);
        bn_svc_cpu_detect.setText("svc方法获取cpu信息");
        bn_svc_cpu_detect.setLayoutParams(new ViewGroup.LayoutParams(
                600,
                ViewGroup.LayoutParams.WRAP_CONTENT));

        Button bn_libc_cpu_detect=new Button(this);
        bn_libc_cpu_detect.setText("libc方法获取cpu信息");
        bn_libc_cpu_detect.setLayoutParams(new ViewGroup.LayoutParams(
                600,
                ViewGroup.LayoutParams.WRAP_CONTENT));

        Button bn_java_cpu_detect=new Button(this);
        bn_java_cpu_detect.setText("java方法获取cpu信息");
        bn_java_cpu_detect.setLayoutParams(new ViewGroup.LayoutParams(
                600,
                ViewGroup.LayoutParams.WRAP_CONTENT));
        layout.addView(bn_java_cpu_detect);
        layout.addView(bn_libc_cpu_detect);
        layout.addView(bn_svc_cpu_detect);
        layout.addView(bn_java_root_detect);
        layout.addView(bn_libc_root_detecct);
        layout.addView(bn_svc_root_detect);


        layout.addView(result_text);
        //setContentView(R.layout.activity_main);
        bn_java_root_detect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                result_text.scrollTo(0,0);
                boolean broot=detectRoot_inJava();
                if(broot) {
                    result_text.setText(" java 方法有 root 不执行add 函数");

                }
                else {

                    result_text.setText(" libC open方法 没有 root 执行 \n" );
                }
            }
        });
        bn_libc_root_detecct.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                result_text.scrollTo(0,0);
                result_text.setText(stringFromJNI());
                boolean broot=detectRoot_libC();
                if(broot) {

                    result_text.setText(" libC open方法 有 root 不执行add 函数");
                    //int x=add(4,5);
                }
                else {

                    result_text.setText(" libC open方法 没有 root 执行 \n" );
                }
            }
        });
        bn_svc_root_detect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                result_text.scrollTo(0,0);
                //boolean broot= CheckSysUtils.isRoot();
                boolean broot= detectRoot_svc();
                if(broot) {
                    result_text.setText(" svc方法有 root 不执行add 函数");

                    //int x=add(4,5);
                }
                else {

                    result_text.setText(" svc 没有 root 执行 可更新运行 \n" );
                }

            }
        });

        bn_svc_cpu_detect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                result_text.scrollTo(0,0);
                    result_text.setText(get_cpuinfo_svc());

            }
        });

        bn_libc_cpu_detect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                result_text.scrollTo(0,0);
                result_text.setText(get_cpuinfo_libc());

            }
        });

        bn_java_cpu_detect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                result_text.scrollTo(0,0);
                result_text.setText(getCpuInfo_java());

            }
        });


    }
    public static String getCpuInfo_java() {
        ShellAdbUtils.CommandResult commandResult = ShellAdbUtils.execCommand("cat /proc/cpuinfo", false);
        return commandResult == null ? "" : commandResult.successMsg;
    }
public boolean detectRoot_inJava()
{
    boolean bExists = false;
    try {
        File f = new File("/system/bin/su");
        bExists = f.exists();
        if(bExists)
            add_log("File exists: " + bExists);
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
    return bExists;
}
    /**
     * A native method that is implemented by the 'native-lib' native library,
     * which is packaged with this application.
     */
    public native String stringFromJNI();

    public native int add(int a, int b);

    public native boolean detectRoot_svc();
    public native boolean detectRoot_libC();
    public native String get_cpuinfo_svc();
    public native String get_cpuinfo_libc();
}